﻿using CoreAnimation;
using CoreGraphics;
using Games.Common;
using Xamarin.Forms.Platform.iOS;

namespace Games.iOS.ControlRenders
{
    public class LineColorButtonRender : VisualElementRenderer<LineColorButton>
    {
        public override void Draw(CGRect rect)
        {
            base.Draw(rect);
            var stack = this.Element;
            CGColor startColor = stack.StartColor.ToCGColor();
            CGColor endColor = stack.EndColor.ToCGColor();
            var tow = stack.IsHorizontal ? stack.Width : 0;
            var toh = stack.IsHorizontal ? 0 : stack.Height;

            var gradientLayer = new CAGradientLayer()
            {
                StartPoint = new CGPoint(0, 0),
                EndPoint = new CGPoint(tow, toh),
            };

            gradientLayer.Frame = rect;
            gradientLayer.Colors = new CGColor[] { startColor, endColor };
            NativeView.Layer.InsertSublayer(gradientLayer, 0);
        }
    }
}